<template>
  <div class="l_personnel">
    <el-card class="box-card">
      <!-- 按钮 -->
      <div slot="header" class="clearfix">
        <el-button type="primary" @click="dialogFormVisible = true">新增</el-button>
      </div>
      <!-- 弹窗 -->
      <el-dialog title="新增客户" :visible.sync="dialogFormVisible">
        <el-form :model="createForm" :rules="createFormRules" ref="createObj">
          <!-- 用户名 -->
          <el-form-item label="用户名" :label-width="formLabelWidth" prop="username">
            <el-input v-model="createForm.username"></el-input>
          </el-form-item>
          <!-- 昵称 -->
          <el-form-item label="昵称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="createForm.name"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
            <el-input v-model="createForm.password"></el-input>
          </el-form-item>
          <!-- 确认密码 -->
          <el-form-item label="确认密码" :label-width="formLabelWidth" prop="confirm_password">
            <el-input v-model="createForm.confirm_password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item class="l_personnel-footer">
            <el-button type="primary" @click="createFn">确 定</el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!-- 内容 -->
      <el-table :data="userlist">
        <el-table-column label="id" prop="id" min-width="30"></el-table-column>
        <el-table-column label="昵称" prop="username" min-width="140"></el-table-column>
        <el-table-column label="姓名" prop="name" min-width="140"></el-table-column>
        <el-table-column label="操作" min-width="110">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-delete" @click="deleteFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- <el-pagination
      background
      layout="prev, pager, next"
      :total="1000">
    </el-pagination> -->
  </div>
</template>

<script>

import util from "../../utils/request"

export default {
  name: "personnel",
  data() {
    return {
      dialogTableVisible: false,
      dialogFormVisible: false,
      createForm: {
        username: '',
        name: '',
        password: '',
        confirm_password: ''
      },
      // 表单验证规则
      createFormRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '昵称不能为空', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能少于三位数', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        confirm_password: [
          { required: true, message: '确认密码不能少于三位数', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px',
      userlist: []
    };
  },
  created() {
    this.tabulationFn();
  },
  methods: {
    handleClick(row) {
      console.log(row);
      this.dialogFormVisible = true;
    },

    // 列表
    tabulationFn() {
      var _this = this;
      var myToken = window.localStorage.getItem("token");
      var uesrObj = {};
      uesrObj.page = 1;
      uesrObj.limit = 10;
      util.call_netA("post", "/admin/admin-user/list", uesrObj, function (data) {
        var res = data.data;
        if (!myToken) {
          _this.$message({
            message: res.msg,
            center: true,
            type: 'success'
          });
          _this.$router.push('/');
        } else {
          if (res.code == 200) {
            var data = res.data.data;
            console.log(data)
            _this.userlist = data;
          }

        }
      }, function (err) { }, myToken)
    },

    // 新增
    createFn() {
      var _this = this;
      var myToken = window.localStorage.getItem("token");
      _this.$refs.createObj.validate(valid => {
        if (!valid) return false;
        var uesrObj = _this.createForm
        util.call_netA("post", "/admin/admin-user/create", uesrObj, function (data) {
          _this.dialogFormVisible = false
          _this.$message.success("操作成功");

          // todo 璐姐 刷新没生效
          _this.refreshData();

        }, function (err) { }, myToken)
      })
    },

    // 删除
    deleteFn(row) {
      var _this = this;
      var myToken = window.localStorage.getItem("token");
      var uesrObj = {};
      uesrObj.id = row.id;
      util.call_netA("delete", "/admin/admin-user/delete", uesrObj, function (data) {
        _this.$message.success("操作成功");

        // todo 璐姐 刷新没生效
        _this.refreshData();
      }, function (err) { }, myToken)
    }

  },
};
</script>

<style scoped>
.l_personnel-footer.el-form-item {
  margin-bottom: 0;
  display: flex;
  justify-content: center;
}

.el-button+.el-button {
  margin-left: 50px;
}

.el-pagination {
  width: 100%;
  height: 50px;
}

.l_personnel-img {
  width: auto;
  height: 30px;
  vertical-align: middle;
}</style>
